//
//  MyNewsVC.m
//  doubleEduStudents
//
//  Created by 盛洋 on 2021/1/13.
//  Copyright © 2021 shengYang. All rights reserved.
//

#import "MyNewsVC.h"
#import "myNewsCell.h"
#import "StudyReportModel.h"

@interface MyNewsVC ()<UITableViewDelegate,UITableViewDataSource>
@property (strong, nonatomic) IBOutlet UIView *back_view;
@property (strong, nonatomic) IBOutlet SYButton *left_btn;
@property (strong, nonatomic) IBOutlet SYButton *right_btn;
@property (strong, nonatomic) IBOutlet UITableView *table;

@end

@implementation MyNewsVC{
    UIView *line_view;
    RequestServices *service;
    NSMutableArray *list_arr;
    NSInteger page;
    NSMutableDictionary *typeDic;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的消息";
    self.left_btn.isChoose = YES;
    self.right_btn.isChoose = NO;
    self.left_btn.titleLabel.font = self.right_btn.titleLabel.font = AutoLabFont2;
    [self.backKuang setHidden:YES];
    APPViewBorderRadius(self.back_view, 10, 1, Base_lineColor);
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.table registerNib:[UINib nibWithNibName:@"myNewsCell" bundle:nil] forCellReuseIdentifier:@"myNewsCell"];
    
    [self.left_btn addTarget:self action:@selector(chooseLeft) forControlEvents:UIControlEventTouchUpInside];
    line_view = [[UIView alloc]init];
    line_view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:line_view];
    [line_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.left_btn.mas_bottom).offset(5);
        make.centerX.equalTo(self.left_btn.mas_centerX);
        make.height.equalTo(@(1));
        make.width.equalTo(@50);
    }];
    
    [self.right_btn addTarget:self action:@selector(chooseRight) forControlEvents:UIControlEventTouchUpInside];
    
    typeDic = [NSMutableDictionary dictionaryWithObject:@"0" forKey:@"type"];
    service = [[RequestServices alloc]initWithDelegate:self];
    service.view = self.view;
    list_arr = [NSMutableArray array];
    [self headerRefresh];
    self.table.mj_header = [MJRefreshHeader headerWithRefreshingBlock:^{
        [self headerRefresh];
    }];
    self.table.mj_footer = [MJRefreshFooter footerWithRefreshingBlock:^{
        [self footReFresh];
    }];
    self.table.mj_footer.hidden = YES;
}

-(void)headerRefresh{
    page = 1;
    [service getNewsList:page type:typeDic[@"type"] tag:8000];
}

-(void)footReFresh{
    page ++;
    [service getNewsList:page type:typeDic[@"type"] tag:8001];

}

-(void)getFinished:(NSDictionary *)obj tag:(long long)tag{
    [self HideLoading];
    [self.table.mj_header endRefreshing ];
    [self.table.mj_footer endRefreshing];
    if (tag == 8000 ) {
        list_arr = [NewsModel mj_objectArrayWithKeyValuesArray:obj[@"list"]];
    }
    if (tag == 8001) {
        NSArray *arr = [NewsModel mj_objectArrayWithKeyValuesArray:obj];
        [list_arr addObjectsFromArray:arr];
    }
    [self.table reloadData];
}

-(void)getError:(NSError *)error tag:(long long)tag{
    [self HideLoading];
    [self.table.mj_header endRefreshing ];
    [self.table.mj_footer endRefreshing];
}


-(void)chooseLeft{
    self.left_btn.isChoose = YES;
    self.right_btn.isChoose = NO;
    [typeDic setValue:@"0" forKey:@"type"];
    [self headerRefresh];

}

-(void)chooseRight{
    line_view.backgroundColor = [UIColor clearColor];
    self.right_btn.isChoose = YES;
    self.left_btn.isChoose = NO;
    [typeDic setValue:@"1" forKey:@"type"];
    [self headerRefresh];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return list_arr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return Is_iPhone ? 80 : 120;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    myNewsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myNewsCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = list_arr[indexPath.row];
    return cell;
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
